package com.mzxy.boxuegu.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mzxy.boxuegu.R;
import com.mzxy.view.MyInfoView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView BottomBarTextCourse;
    private TextView BottomBarTextExercise;
    private TextView BottomBarTextMyinfo;
    private ImageView bottomBarImageCourse;
    private ImageView bottomBarImageExercise;
    private ImageView bottomBarImageMyinfo;
    private RelativeLayout rlBottomBarCourseBtn;
    private RelativeLayout rlBottomBarExercisesBtn;
    private RelativeLayout rlBottomBarMyinfoBtn;
    private FrameLayout flMainBody;
    private TextView tvBack;
    private TextView tvMainTitle;
    private RelativeLayout titleBar;
    private MyInfoView myInfoView;
    private View courceView;
    private View exercisesView;

    //这是通过as提交的代码
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // btn1 = (Button) findViewById(R.id.btn1);
        initView();
        initEvent();
        findViewById(R.id.title_bar).setBackgroundColor(Color.parseColor("#30B4FF"));
        tvBack.setVisibility(View.GONE);
        selectDisplayView(0);

    }

    private void initEvent() {
        rlBottomBarCourseBtn.setOnClickListener(this);
        rlBottomBarExercisesBtn.setOnClickListener(this);
        rlBottomBarMyinfoBtn.setOnClickListener(this);

    }

    private void initView() {
        rlBottomBarCourseBtn = (RelativeLayout) findViewById(R.id.bottom_bar_course_btn);
        rlBottomBarExercisesBtn = (RelativeLayout) findViewById(R.id.bottom_bar_exercises_btn);
        rlBottomBarMyinfoBtn = (RelativeLayout) findViewById(R.id.bottom_bar_myinfo_btn);
        BottomBarTextCourse = (TextView) findViewById(R.id.bottom_bar_text_course);
        BottomBarTextExercise = (TextView) findViewById(R.id.bottom_bar_text_exercises);
        BottomBarTextMyinfo = (TextView) findViewById(R.id.bottom_bar_text_myinfo);
        bottomBarImageCourse = (ImageView) findViewById(R.id.bottom_bar_image_course);
        bottomBarImageExercise = (ImageView) findViewById(R.id.bottom_bar_image_exercises);
        bottomBarImageMyinfo = (ImageView) findViewById(R.id.bottom_bar_image_myinfo);
        flMainBody = (FrameLayout) findViewById(R.id.main_body);
        tvBack = (TextView) findViewById(R.id.tv_back);
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        titleBar = (RelativeLayout) findViewById(R.id.title_bar);

    }
    public void toRegister(View view){
        startActivity(new Intent(this,RegisterActivity.class));
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bottom_bar_course_btn:
                selectDisplayView(0);
                break;
            case R.id.bottom_bar_exercises_btn:
                selectDisplayView(1);
                break;
            case R.id.bottom_bar_myinfo_btn:
                selectDisplayView(2);
                break;
        }
    }

    private void selectDisplayView(int index) {
        titleBar.setVisibility(View.VISIBLE);
        removeAllView();
        createView(index);
        setSelectedStatus(index);
    }
    private void removeAllView(){
        for (int i = 0 ;i <flMainBody.getChildCount();i++){
            flMainBody.getChildAt(i).setVisibility(View.GONE);
        }
    }

    private void createView(int index) {
        switch (index) {
            case 0:
               if (courceView == null) {
                   courceView = LayoutInflater.from(this).inflate(R.layout.testv1,null);
                   flMainBody.addView(courceView);
               }
                courceView.setVisibility(View.VISIBLE);
               break;

            case 1:
                if (exercisesView == null) {
                    exercisesView = LayoutInflater.from(this).inflate(R.layout.testv2,null);
                    flMainBody.addView(exercisesView);
                }
                exercisesView.setVisibility(View.VISIBLE);
                break;


            case 2:
                if (myInfoView == null){
                                  myInfoView = new MyInfoView(this);
                                  flMainBody.addView(myInfoView.getView());
                              }
                              myInfoView.showView();
                              break;
                        }
                }





    private void clearBottomImageState() {
        BottomBarTextCourse.setTextColor(Color.parseColor("#666666"));
        BottomBarTextExercise.setTextColor(Color.parseColor("#666666"));
        BottomBarTextMyinfo.setTextColor(Color.parseColor("#666666"));
        bottomBarImageCourse.setImageResource(R.drawable.main_course_icon);
        bottomBarImageExercise.setImageResource(R.drawable.main_exercises_icon);
        bottomBarImageMyinfo.setImageResource(R.drawable.main_my_icon);

    }

    public void setSelectedStatus(int index) {
        clearBottomImageState();
        switch (index) {
            case 0:
                BottomBarTextCourse.setTextColor(Color.parseColor("#0097F7"));
                bottomBarImageCourse.setImageResource(R.drawable.main_course_icon_selected);
                tvMainTitle.setText("课程");
                break;
            case 1:
                BottomBarTextExercise.setTextColor(Color.parseColor("#0097F7"));
                bottomBarImageExercise.setImageResource(R.drawable.main_exercises_icon_selected);
                tvMainTitle.setText("习题");
                break;
            case 2:
                BottomBarTextMyinfo.setTextColor(Color.parseColor("#0097F7"));
                bottomBarImageMyinfo.setImageResource(R.drawable.main_my_icon_selected);
                tvMainTitle.setText("我");
                break;
        }
    }


    protected long exitTime;

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (System.currentTimeMillis() - exitTime < 1000) {
                finish();
                System.exit(0);
            } else {
                exitTime = System.currentTimeMillis();
                Toast.makeText(this, "再按一次推出博学谷", Toast.LENGTH_SHORT).show();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
       /* @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if((requestCode == Constants.MYINFO2LOGIN_REQUEST_CODE) &&
                    (resultCode == Constants.LOGIN2MYINFO_RESULT_CODE)){
                //这次登陆是从 “我”的界面发起的，并且数据数回传给“我”
                //在这里 获取回传的 用户名，并且设置到MyInfoView里面去
                String username = data.getStringExtra("username");
                myInfoView.setLoginParam(username);
            }else if((requestCode == Constants.MYINFO2SETTING_REQUEST_CODE) &&
                    (resultCode == Constants.SETTING2MYINFO_RESULT_CODE)){
                //代表从设置界面跳转过来的页面
                boolean exitLogin = data.getBooleanExtra("exitLogin",false);
                if(exitLogin){
                    myInfoView.setLoginParam(null);
                }
            }

0
*/
        }
}

